package compile_and_download

import (
	"log"
	"os"
	"os/exec"
	"test/unit_function/file"
	"test/unit_function/sshdata"
)

type Rk3568Benchmark struct {
	BuildTarget
}

// Compile 编译任务：上传脚本，执行脚本，下载产物
func (receiver *Rk3568Benchmark) Compile() error {
	client := sshdata.GetConnection()
	defer client.Close()
	result := sshdata.UploadFile("/mnt/data/ywx1293154/script_benchmark.sh", "D:/auto_test/test_script_for_openharmony/script/script_benchmark.sh", client, "0744")
	if !result {
		log.Println("upload file script_benchmark failed")
	}
	log.Println("uploadFile script_benchmark.sh succeed")
	result = sshdata.UploadFile("/mnt/data/ywx1293154/benchmark.patch", "D:/auto_test/test_script_for_openharmony/script/benchmark.patch", client, "0744")
	if !result {
		log.Println("upload file benchmark.patch failed")
	}
	log.Println("upload file benchmark.patch succeed")
	result = sshdata.UploadFile("/mnt/data/ywx1293154/benchmark_build.patch", "D:/auto_test/test_script_for_openharmony/script/benchmark_build.patch", client, "0744")
	if !result {
		log.Println("upload file benchmark_build.patch failed")
	}
	log.Println("upload file benchmark_build.patch succeed")
	result = sshdata.UploadFile("/mnt/data/ywx1293154/benchmark_musl_conf.patch", "D:/auto_test/test_script_for_openharmony/script/benchmark_musl_conf.patch", client, "0744")
	if !result {
		log.Println("upload file benchmark_musl_conf.patch failed")
	}
	//自动上传tmp.patch文件存在问题
	log.Println("upload file benchmark_musl_conf.patch succeed")
	session, err := client.NewSession()
	if err != nil {
		log.Println("Rk3568Benchmark newSession failed, error=", err)
		return err
	}
	defer session.Close()
	log.Println("executing script_benchmark.sh")
	err = session.Run("~/script_benchmark.sh > ~/script_benchmark.log")
	if err != nil {
		log.Println("Rk3568Benchmark run script failed, error=", err)
		return err
	}
	log.Println("executing script_benchmark.sh succeed!")
	return nil
}
func (receiver *Rk3568Benchmark) Download() error {
	// 下载文件并解压
	client := sshdata.GetConnection()
	defer client.Close()
	sshdata.DownloadFile("/mnt/data/ywx1293154/openharmony/out/rk3568/thirdparty/benchmark.tar.gz", "D:/auto_test/benchmark/benchmark.tar.gz", client)
	cmd := exec.Command("tar", "-zxvf", "benchmark.tar.gz")
	cmd.Dir = "D:/auto_test/benchmark"
	err := cmd.Run()
	if err != nil {
		log.Println("error occurred when executing tar -zxvf benchmark.tar.gz ", err)
		return err
	}
	log.Println("executing tar -zxvf benchmark.tar.gz succeed")
	log.Println("downloading benchmark succeed!")
	return nil
}

func (receiver *Rk3568Benchmark) Execute() error {
	// 检查是否存在之前的结果
	exists := receiver.checkPreviousExists()
	// 如果存在，需要进行重命名
	if exists {
		receiver.rename()
	}
	// 1.将可执行文件与sh脚本放入/data/local/tmp/benchmark
	cmd := exec.Command("hdc", "file", "send", "script/benchmark_terminal_device.sh", "/data/local/tmp/benchmark/")
	cmd.Dir = "D:/auto_test/test_script_for_openharmony"
	log.Println("executing", "hdc", "file", "send", "script/benchmark_terminal_device.sh", "/data/local/tmp/benchmark/")
	err := cmd.Run()
	if err != nil {
		log.Println("hdc", "file", "send", "script/benchmark_terminal_device.sh", "/data/local/tmp/benchmark/", "failed", err)
		return err
	}
	cmd = exec.Command("hdc", "file", "send", "bionic_benchmark", "/data/local/tmp/benchmark/")
	cmd.Dir = "D:/auto_test/benchmark/musl/"
	log.Println("executing", "hdc", "file", "send", "bionic_benchmark", "/data/local/tmp/benchmark/")
	err = cmd.Run()
	if err != nil {
		log.Println("hdc", "file", "send", "/bionic_benchmark", "/data/local/tmp/benchmark/", "failed", err)
		return err
	}
	cmd = exec.Command("hdc", "file", "send", "musl_benchmark", "/data/local/tmp/benchmark/")
	cmd.Dir = "D:/auto_test/benchmark/musl"
	log.Println("executing", "hdc", "file", "send", "musl_benchmark", "/data/local/tmp/benchmark/")
	err = cmd.Run()
	if err != nil {
		log.Println("hdc", "file", "send", "D:/auto_test/benchmark/musl/musl_benchmark", "/data/local/tmp/benchmark/", "failed", err)
		return err
	}
	// 2.执行可执行文件
	cmd = exec.Command("hdc", "shell", "chmod +x /data/local/tmp/benchmark/*")
	log.Println("executing", "hdc", "shell", "chmod +x /data/local/tmp/benchmark/*")
	err = cmd.Run()
	if err != nil {
		log.Println("hdc", "shell", "chmod +x /data/local/tmp/benchmark/*", "failed", err)
		return err
	}
	cmd = exec.Command("hdc", "shell", "/data/local/tmp/benchmark/benchmark_terminal_device.sh")
	log.Println("executing", "hdc", "shell", "/data/local/tmp/benchmark/benchmark_terminal_device.sh")
	err = cmd.Run()
	if err != nil {
		log.Println("hdc", "shell", "/data/local/tmp/benchmark/benchmark_terminal_device.sh", "failed", err)
		return err
	}
	// 3.将结果取出
	cmd = exec.Command("hdc", "file", "recv", "/data/local/tmp/benchmark/bionic_benchmark.log")
	log.Println("executing", "hdc", "file", "recv", "/data/local/tmp/benchmark/bionic_benchmark.log")
	cmd.Dir = "D:/auto_test/benchmark/result"
	err = cmd.Run()
	if err != nil {
		log.Println("hdc", "file", "recv", "/data/local/tmp/benchmark/bionic_benchmark.log", "failed", err)
		return err
	}
	cmd = exec.Command("hdc", "file", "recv", "/data/local/tmp/benchmark/musl_benchmark.log")
	log.Println("executing", "hdc", "file", "recv", "/data/local/tmp/benchmark/musl_benchmark.log")
	cmd.Dir = "D:/auto_test/benchmark/result"
	err = cmd.Run()
	if err != nil {
		log.Println("hdc", "file", "recv", "/data/local/tmp/benchmark/musl_benchmark.log", "failed", err)
		return err
	}
	cmd = exec.Command("tar", "-zcvf", "benchmark.tar.gz", "result")
	log.Println("executing", "tar", "-zcvf", "benchmark.tar.gz", "result")
	cmd.Dir = "D:/auto_test/benchmark"
	err = cmd.Run()
	if err != nil {
		log.Println("tar", "-zcvf", "benchmark.tar.gz", "result", "failed", err)
		return err
	}
	err = os.Rename("D:/auto_test/benchmark/benchmark.tar.gz", "D:/auto_test/result/benchmark.tar.gz")
	if err != nil {
		return err
	}
	file.Copy("D:/auto_test/benchmark/result/musl_benchmark.log", "D:/auto_test/result/benchmark/musl_benchmark.log")
	file.Copy("D:/auto_test/benchmark/result/bionic_benchmark.log", "D:/auto_test/result/benchmark/bionic_benchmark.log")
	// 如果有前一天的数据，需要通过脚本生成对比结果，然后一并copy至结果中
	if exists {
		compareResult()
		file.Copy("D:/auto_test/benchmark/result/yesterday_musl_benchmark.log", "D:/auto_test/result/benchmark/yesterday_musl_benchmark.log")
		file.Copy("D:/auto_test/benchmark/result/yesterday_bionic_benchmark.log", "D:/auto_test/result/benchmark/yesterday_bionic_benchmark.log")
	}
	return nil
}

func (receiver *Rk3568Benchmark) checkPreviousExists() bool {
	// 仅判断是否存在之前的结果
	exists := file.FileExists("D:/auto_test/benchmark/result/musl_benchmark.log")
	exists2 := file.FileExists("D:/auto_test/benchmark/result/bionic_benchmark.log")
	return exists || exists2
}

func (receiver *Rk3568Benchmark) rename() {
	if file.FileExists("D:/auto_test/benchmark/result/musl_benchmark.log") {
		err := os.Rename("D:/auto_test/benchmark/result/musl_benchmark.log", "D:/auto_test/benchmark/result/yesterday_musl_benchmark.log")
		if err != nil {
			log.Println("rename failed musl_benchmark.log -> yesterday_musl_benchmark.log")
		}
	}
	if file.FileExists("D:/auto_test/benchmark/result/bionic_benchmark.log") {
		err := os.Rename("D:/auto_test/benchmark/result/bionic_benchmark.log", "D:/auto_test/benchmark/result/yesterday_bionic_benchmark.log")
		if err != nil {
			log.Println("rename failed bionic_benchmark.log -> yesterday_bionic_benchmark.log")
		}
	}
}

func compareResult() {
	if file.FileExists("D:/auto_test/benchmark/result/yesterday_musl_benchmark.log") {
		cmd := exec.Command("python", "D:/auto_test/test_script_for_openharmony/pyscript/compare_result.py", "--before", "yesterday_musl_benchmark.log", "--after", "musl_benchmark.log", "--range", "1", "--filename", "musl_compare_result")
		cmd.Dir = "D:/auto_test/benchmark/result"
		err := cmd.Run()
		if err != nil {
			log.Println("compare failed musl_benchmark.log vs yesterday_musl_benchmark.log err msg:", err)
		}
	}
	if file.FileExists("D:/auto_test/benchmark/result/yesterday_bionic_benchmark.log") {
		cmd := exec.Command("python", "D:/auto_test/test_script_for_openharmony/pyscript/compare_result.py", "--before", "yesterday_bionic_benchmark.log", "--after", "bionic_benchmark.log", "--range", "1", "--filename", "bionic_compare_result")
		cmd.Dir = "D:/auto_test/benchmark/result"
		err := cmd.Run()
		if err != nil {
			log.Println("compare failed bionic_benchmark.log vs yesterday_bionic_benchmark.log err msg:", err)
		}
	}
}
